package com.javastar.modules.user.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.javastar.common.annotation.Login;
import com.javastar.common.annotation.LoginUser;
import com.javastar.common.result.Result;
import com.javastar.modules.user.entity.UserEntity;
import com.javastar.modules.user.service.UserService;
import com.javastar.modules.user.to.UsernameLoginTO;
import com.javastar.modules.user.vo.ReturnUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;

/**
 * 用户
 *
 * @author zxx
 * @date 2023/7/23
 */
@RestController
@RequestMapping("user")
@Api(tags = "用户")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("phoneOrEmailAndPasswordLogin")
    @ApiOperation(value = "手机号或邮箱和密码登陆")
    @ApiOperationSupport(order = 1)
    @ApiResponses({
            @ApiResponse(code = 10101, message = "用户名或密码错误"),
            @ApiResponse(code = 10102, message = "用户已被禁用，请联系管理员")
    })
    public Result<ReturnUserVO> phoneOrEmailAndPasswordLogin(@RequestBody UsernameLoginTO usernameLoginTO, @ApiIgnore HttpServletResponse response) {
        ReturnUserVO returnUserVO = userService.phoneOrEmailAndPasswordLogin(usernameLoginTO, response);
        return new Result<ReturnUserVO>().ok(returnUserVO);
    }

    @GetMapping("test")
    @ApiOperation(value = "测试接口")
    @ApiOperationSupport(order = 2)
    @Login
    public Result<UserEntity> test(@ApiIgnore @LoginUser UserEntity user) {
        return new Result<UserEntity>().ok(user);
    }
}
